package Show;
use Mojo::Base -base;
use Data::Dumper;
use Show::Get;
use Show::Post;

has scalar_tag => "div";
has scalar_class => "scalar";

has hash_tag => "div";
has hash_class => "hash";

has array_tag => "div";
has array_class => "array";


sub show{
 my $se = shift;
 my $data = shift or return '';
# die Dumper $data;
 my %pa = @_;
 if ( !ref $data ){
    return "<".$se->scalar_tag
	. " class='". ( $pa{-class}||$se->scalar_class ) 
	."'>$data</".$se->scalar_tag.">\n";
 }
 elsif( ref $data eq 'ARRAY' ){
    my $rv="<".$se->array_tag." class='".$se->array_class."'>\n";
    for my $el ( @$data ){
	$rv .= $se->show( $el||'' );
    }
    $rv .= "</".$se->array_tag.">";
    return $rv;
 }
 elsif( ref $data eq 'HASH' ){
    my $rv = "<".$se->hash_tag." class='".$se->hash_class."'>\n";
    for my $k ( keys %$data ){
	my $v = $data->{$k};
	$rv .= $se->show( $v||'', -class=>$k );
    }
    $rv .= "</".$se->hash_tag.">";
    return $rv;
 }
 elsif( ref $data eq 'Show::Get' ){
    my $rv = "<div class='Get'>\n";
    $rv .= "<a href='".$data->controller->url_for( $data->r_name, $data->r_params )."'";
    $rv .=" class='". $data->r_name."'>";
    $rv .= $data->human . "</a>";
    $rv .= "</div>";
    return  $rv;
 }
 elsif( ref $data eq 'Show::Post' ){
    my $rv = "<div class='Post'>\n";
    #die $data->controller->url_for( $data->r_name, $data->r_params );
    $rv .= "<form action = ". $data->controller->url_for( $data->r_name, $data->r_params ) 
    ." method='post'"
    ." class=".$data->r_name.">\n";
    my %fields = $data->q_params || $data->r_params;
    for my $fn ( keys %fields ){
	$rv .= "<div class='".$data->r_name."'>\n";
	$rv .="<input type='text' name='$fn' value='$fields{$fn}' "
	    ."class='".$data->r_name."' />\n";
	$rv .= "<div>$fn</div>\n";
	$rv .= "</div>\n";
    }
    $rv .= "<div class='".$data->r_name."'>";
    $rv .="<input type='submit' class='".$data->r_name."'>";
    $rv .="</div>\n";
    $rv .= "</form>\n";
    $rv .= "</div>\n";
    return  $rv;
 } 
 else{ return Dumper $data }
}

1;